// pages/commentList/commentList.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		goodsId: 0,
		page: 1,
		hasMore: true,
		data: [],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.data.goodsId = options.id;
		this.onPullDownRefresh();
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		this.loadData(1).finally(() => {
			wx.stopPullDownRefresh();
		});
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.hasMore) return;

		const page = this.data.page + 1;
		this.loadData(page);
	},

	// 加载商城列表
	loadData: function(page = 1) {
		return wx.http.get('api/Comment/lists', {
			page: page,
			goods_id: this.data.goodsId,
			scoreType: '',
		}).then((res) => {
			res = res.data.data.list;
			const hasMore = res.data.length >= res.per_page;
			const data = page > 1 ? this.data.data.concat(res.data) : res.data;
			this.setData({
				page: page,
				hasMore: hasMore,
				data: data,
			});
			return res.data;
		}, (err) => {
			console.warn("api/Comment/lists", err)
		});
	},

	// 预览图片
	previewTap: function(e) {
		const idx1 = e.currentTarget.dataset.idx,
			idx2 = e.target.dataset.idx;
		if (idx1 === undefined || idx2 === undefined) return;

		const images = this.data.data[idx1].image.map(it => it.file_path);
		wx.previewImage({
			urls: images,
			current: images[idx2]
		});
	}
});
